package com.edu.kindergarten.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.edu.kindergarten.R;
import com.edu.kindergarten.utils.LogUtils;

public class GrowthArchiveActivity extends BaseFragmentActivity {
	
	private static final String TAG = "GrowthArchiveActivity";
	
	private int tabIds[] = new int[]{
			R.id.home_tab_main,
			R.id.home_tab_search,
			R.id.home_tab_set,
			R.id.home_tab_category,
	};
	
	private GrowthArchiveHomeFragment GrowthArchiveHomeTab;
	private TeacherHomeFragment mTab2;
	private GartenHomeFragment mTab3;
	private GartenHomeFragment mTab4;

	@Override
	public void onAttachFragment(Fragment fragment) {
		// TODO Auto-generated method stub
		super.onAttachFragment(fragment);
		LogUtils.d(TAG,"onAttachFragment");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		LogUtils.d(TAG,"onDestroy");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		LogUtils.d(TAG,"onPause");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		LogUtils.d(TAG,"onResume");
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		LogUtils.d(TAG,"onStart");
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		LogUtils.d(TAG,"onStop");
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LogUtils.d(TAG,"onCreate");
		setContentView(R.layout.layout_growth_archive_activity);
		
		RadioGroup tabButtonGroup = (RadioGroup) findViewById(R.id.bottom_bar);
		tabButtonGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				for (int i = 0; i < tabIds.length; i++) {
					if (tabIds[i] == checkedId) {
						setSelection(i);
						break;
					}
				}
			}
		});
		
		setSelection(0);
	}
	
	private void setSelection(int position){
		
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		hideAllFragments(ft);
		
		switch (position) {
		case 0:
			if (GrowthArchiveHomeTab == null) {
				GrowthArchiveHomeTab = new GrowthArchiveHomeFragment();
				ft.add(R.id.content, GrowthArchiveHomeTab);
			}else {
				ft.show(GrowthArchiveHomeTab);
			}
			break;
			
		case 1:
			if (mTab2 == null) {
				mTab2 = new TeacherHomeFragment();
				ft.add(R.id.content, mTab2);
			}else {
				ft.show(mTab2);
			}
			break;
			
		case 2:
			if (mTab3 == null) {
				mTab3 = new GartenHomeFragment();
				ft.add(R.id.content, mTab3);
			}else {
				ft.show(mTab3);
			}
			break;
		case 3:
			if (mTab4 == null) {
				mTab4 = new GartenHomeFragment();
				ft.add(R.id.content, mTab4);
			}else {
				ft.show(mTab4);
			}
			break;

		default:
			break;
		}
		
		ft.commit();
	}
	
	private void hideAllFragments(FragmentTransaction ft){
		if (GrowthArchiveHomeTab != null) {
			ft.hide(GrowthArchiveHomeTab);
		}
		
		if (mTab2 != null) {
			ft.hide(mTab2);
		}
		
		if (mTab3 != null) {
			ft.hide(mTab3);
		}
		
		if (mTab4 != null) {
			ft.hide(mTab4);
		}
	}

}